package com.ezhuan.task.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ezhuan.common.dao.impl.BaseDaoImpl;
import com.ezhuan.common.util.PageInfoUtil;
import com.ezhuan.task.dao.IATaskDao;
import com.ezhuan.task.pojo.ATask;
import com.ezhuan.users.pojo.AUsersWithdraw;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class ATaskDaoImpl extends BaseDaoImpl<ATask> implements IATaskDao
{
	private ATask task = new ATask() ; 
	
	@Override
	public ATask findOneDao(Map<String, Object> condMap)
	{
		/* 拼装SQL语句 */
		StringBuffer sqlSb = new StringBuffer();
		List<Object> paramsList = new ArrayList<Object>();
		/* 拼装查询条件 */
		sqlSb.append("select * from "+ task.getTableName() +" where 1 = 1 ");
		if (condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			/* 按照ID查询 */
			sqlSb.append(" and id = ? ");
			paramsList.add(condMap.get("id"));
		}else if (condMap.get("thirdSn") != null && !"".equalsIgnoreCase(condMap.get("thirdSn") + ""))
		{
			/* 按照ID查询 */
			sqlSb.append(" and thirdSn = ? ");
			paramsList.add(condMap.get("thirdSn"));
		}else
		{
			/* 木有任何条件,直接返回Null */
			return null ; 
		}
		Record record = Db.findFirst(sqlSb.toString(), paramsList.toArray());
		if (record != null)
		{
			/* 用户信息 */
			ATask task = new ATask();
			return task.parseRecord(record);
		}
		return null;
	}

	@Override
	public List<ATask> findCondListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 拼装SQL语句 */
		StringBuffer sqlSb = new StringBuffer();
		List<Object> paramsList = new ArrayList<Object>();
		/* 拼装查询条件 */
		sqlSb.append("select * from "+ task.getTableName() +" where 1 = 1 ");
		/* 任务类型 */
		if (condMap.get("taskType") != null && !"".equalsIgnoreCase(condMap.get("taskType") + ""))
		{
			sqlSb.append(" and taskType = ? ");
			paramsList.add(condMap.get("taskType"));
		}
		/* 分类Id */
		if (condMap.get("cateId") != null && !"".equalsIgnoreCase(condMap.get("cateId") + ""))
		{
			sqlSb.append(" and cateId = ? ");
			paramsList.add(condMap.get("cateId"));
		}
		/* 渠道Id */
		if (condMap.get("channelId") != null && !"".equalsIgnoreCase(condMap.get("channelId") + ""))
		{
			sqlSb.append(" and channelId = ? ");
			paramsList.add(condMap.get("channelId"));
		}
		
		List<Record> recordList = Collections.EMPTY_LIST ; 
		/* 分页 */
		if (pageInfoUtil != null)
		{
			recordList = this.findByPage(pageInfoUtil, sqlSb.toString(), paramsList);
		}else
		{
			recordList = this.find(sqlSb.toString(), paramsList);
		}

		/* 存储结果 */
		List<ATask> usersWithdrawList = new ArrayList<ATask>();
		for (Iterator iterator = recordList.iterator(); iterator.hasNext();)
		{
			Record record = (Record) iterator.next();
			/* 用户信息 */
			ATask task = new ATask();
			task = task.parseRecord(record);
			usersWithdrawList.add(task);
		}
		return usersWithdrawList;
	}

}
